package com.agileai.pm.module.base.handler;

import java.util.*;

import com.agileai.domain.*;
import com.agileai.hotweb.bizmoduler.core.TreeManage;
import com.agileai.hotweb.controller.core.TreeManageHandler;
import com.agileai.hotweb.domain.*;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.module.base.service.ProjectFunctionTreeManage;
import com.agileai.util.*;

public class ProjectFunctionTreeManageHandler
        extends TreeManageHandler {
    public ProjectFunctionTreeManageHandler() {
        super();
        this.serviceId = buildServiceId(ProjectFunctionTreeManage.class);
        this.nodeIdField = "PF_ID";
        this.nodePIdField = "PF_PID";
        this.moveUpErrorMsg = "该节点是第一个节点，不能上移！";
        this.moveDownErrorMsg = "该节点是最后一个节点，不能下移！";
        this.deleteErrorMsg = "该节点还有子节点，不能删除！";
    }
    
    public ViewRenderer prepareDisplay(DataParam param) {
		String nodeId = param.get(this.nodeIdField);
		if (StringUtil.isNullOrEmpty(nodeId)){
			nodeId = provideDefaultNodeId(param);
		}
		TreeBuilder treeBuilder = provideTreeBuilder(param);
		TreeModel treeModel = treeBuilder.buildTreeModel();
		
		String rootId = param.getString("PF_ID");
		if("00000000-0000-0000-00000000000000000".equals(rootId)){
			this.setAttribute("isDisplay", false);
		}else{
			this.setAttribute("isDisplay", true);
		}
		
		TreeManage service = this.getService();
		DataParam queryParam = new DataParam(this.nodeIdField,nodeId);
		DataRow record = service.queryCurrentRecord(queryParam);
		this.setAttributes(record);	
		this.processPageAttributes(param);
		this.setAttribute(this.nodeIdField, nodeId);
		this.setAttribute("isRootNode",String.valueOf(isRootNode(param)));
		
		String menuTreeSyntax = this.getTreeSyntax(param,treeModel,new StringBuffer());
		this.setAttribute("menuTreeSyntax", menuTreeSyntax);
		param.put("PF_PID", record.get("PF_PID"));
		param.put("PF_NAME", record.get("PF_NAME"));
		param.put("PF_CHARGE", record.get("PF_CHARGE"));
		param.put("PF_DESC", record.get("PF_DESC"));
		this.setAttributes(param);
		return new LocalRenderer(getPage());
	}

    protected TreeBuilder provideTreeBuilder(DataParam param) {
        ProjectFunctionTreeManage service = this.getService();
        List<DataRow> menuRecords = service.findTreeRecords(param);
        TreeBuilder treeBuilder = new TreeBuilder(menuRecords, "PF_ID","PF_NAME", "PF_PID");

        return treeBuilder;
    }

    protected void processPageAttributes(DataParam param) {
    }

    protected String provideDefaultNodeId(DataParam param) {
        return "00000000-0000-0000-00000000000000000";
    }

    protected boolean isRootNode(DataParam param) {
        boolean result = true;
        String nodeId = param.get(this.nodeIdField,
                                  this.provideDefaultNodeId(param));
        DataParam queryParam = new DataParam(this.nodeIdField, nodeId);
        DataRow row = this.getService().queryCurrentRecord(queryParam);

        if (row == null) {
            result = false;
        } else {
            String parentId = row.stringValue("PF_PID");
            result = StringUtil.isNullOrEmpty(parentId);
        }

        return result;
    }

    protected ProjectFunctionTreeManage getService() {
        return (ProjectFunctionTreeManage) this.lookupService(this.getServiceId());
    }
}
